package com.jie.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jie.commonutils.ResultEntity;
import com.jie.staservice.client.UcenterClient;
import com.jie.staservice.entity.StatisticsDaily;
import com.jie.staservice.mapper.StatisticsDailyMapper;
import com.jie.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author xzj
 * @since 2020-10-18
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    private final UcenterClient ucenterClient;

    @Autowired
    public StatisticsDailyServiceImpl(UcenterClient ucenterClient) {
        this.ucenterClient = ucenterClient;
    }

    @Override
    public void registerCount(String date) {
        // 远程调用得到某一天的注册人数
        ResultEntity resultEntity = ucenterClient.countRegister(date);
        Integer countRegister = (Integer) resultEntity.getData().get("countRegister");

        // 添加前删除表中相同日期的数据(this.saveOrUpdate应该也可以, 没有试)
        baseMapper.delete((new QueryWrapper<StatisticsDaily>()).eq("date_calculated", date));

        // 把获取数据添加到数据库统计分析表里
        // PS: 只获取注册人数和统计日期, 其它数据随机生成, 做一个模拟
        StatisticsDaily sta = new StatisticsDaily();
        sta.setRegisterNum(countRegister);  // 注册人数
        sta.setDateCalculated(date);        // 统计日期
        // 其它值做模拟
        sta.setVideoViewNum(RandomUtils.nextInt(100, 200));
        sta.setLoginNum(RandomUtils.nextInt(100, 200));
        sta.setCourseNum(RandomUtils.nextInt(100, 200));

        baseMapper.insert(sta);
    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        // 根据条件查出对应字段的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        wrapper.select("date_calculated", type);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        // 前端要求数据json结构, 对应后端Java代码是List集合
        List<String> dateList = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();
        for (StatisticsDaily statisticsDaily : staList) {
            dateList.add(statisticsDaily.getDateCalculated());
            switch (type) {
                case "login_num":
                    numList.add(statisticsDaily.getLoginNum());
                    break;
                case "register_num":
                    numList.add(statisticsDaily.getRegisterNum());
                    break;
                case "video_view_num":
                    numList.add(statisticsDaily.getVideoViewNum());
                    break;
                case "course_num":
                    numList.add(statisticsDaily.getCourseNum());
                    break;
                default:
                    break;
            }
        }

        // 将数据放入map中并返回
        Map<String, Object> map = new HashMap<>();
        map.put("date_calculatedList", dateList);
        map.put("numDataList", numList);
        return map;
    }


}
